using System.IO;
using FluentNHibernate;
using FluentNHibernate.Cfg.Db;
using Lucene.Net.Analysis;
using NHibernate;
using NHibernate.Search.Event;
using NHibernate.Search.Store;
using Wikey.Domain.Components;
using Wikey.Domain.Mappings;

namespace Wikey.Tests
{
    public class TestCase
    {
        //protected ISession session;

        public TestCase()
        {
            FindAppPath();
            CreateHibernateFolder();
            Configure();
        }

        protected ISessionFactory _sessionFactory; 

        private void Configure()
        {
            IPersistenceConfigurer persistenceConfigurer =
                MsSqlConfiguration
                    .MsSql2005.ConnectionString(cs=> cs.Username("wikey").Password("wikey").Server(
                        @".\SQL2008").Database("Wikey"))
                    .ShowSql();


            // initialize nhibernate with persistance configurer properties
            NHibernate.Cfg.Configuration cfg = persistenceConfigurer.ConfigureProperties(
                new NHibernate.Cfg.Configuration());

            cfg.SetProperty("hibernate.search.default.directory_provider",
                                typeof(FSDirectoryProvider).AssemblyQualifiedName);
            cfg.SetProperty(NHibernate.Search.Environment.AnalyzerClass,
                            typeof(StopAnalyzer).AssemblyQualifiedName);

            cfg.SetListener(NHibernate.Event.ListenerType.PostUpdate, new FullTextIndexEventListener());
            cfg.SetListener(NHibernate.Event.ListenerType.PostInsert, new FullTextIndexEventListener());
            cfg.SetListener(NHibernate.Event.ListenerType.PostDelete, new FullTextIndexEventListener());

            // add mappings definition to nhibernate configuration
            var persistenceModel = new PersistenceModel();
            
            persistenceModel.AddMappingsFromAssembly(typeof(PageMap).Assembly);
            
            persistenceModel.Conventions.AddFromAssemblyOf<TableNameConvention>(); 
            persistenceModel.Configure(cfg);
            
            persistenceModel.WriteMappingsTo(MappingsPath);
            // set session factory field which is to be used in tests
            _sessionFactory = cfg.BuildSessionFactory();
        }

        private string AppPath = string.Empty; 
         
        private void FindAppPath()
        {
            AppPath = new DirectoryInfo(".").FullName;

        }

        private string MappingsPath = string.Empty;

        private void CreateHibernateFolder()
        {
            MappingsPath = AppPath + "\\NHibernateMappings";

            if (!Directory.Exists(MappingsPath))
            {
                Directory.CreateDirectory(MappingsPath);
            }

            
        }

        private string GetMappingsPath()
        {
            string fileName = AppPath + "\\Wikey.Domain.Mappings.dll";

            return fileName;

        }

       
    }
}